﻿namespace StudyCompanion.Commons.Page;

internal class PageResult
{
}

/// <summary>
///     分页对象
/// </summary>
/// <typeparam name="T"></typeparam>
public class PageResult<T>
{
    private IReadOnlyList<T> _data = Array.Empty<T>();

    public PageResult()
    {
    }

    public PageResult(IPageSearch search)
        : this(search, default, default)
    {
    }

    public PageResult(IPageSearch search, List<T> data, int count)
        : this(search.PageIndex, search.PageSize, data, count)
    {
    }

    public PageResult(int pageIndex, int pageSize, List<T> data, int count)
    {
        PageIndex = pageIndex;
        PageSize = pageSize;
        TotalCount = count;
        Data = data;
    }

    public IReadOnlyList<T> Data
    {
        get => _data;
        set
        {
            if (value != null) _data = value;
        }
    }

    /// <summary>
    ///     当前页记录数量
    /// </summary>
    public int RowsCount => _data.Count;

    /// <summary>
    ///     当前页码
    /// </summary>
    public int PageIndex { get; set; }

    /// <summary>
    ///     每页记录数量
    /// </summary>
    public int PageSize { get; set; }

    /// <summary>
    ///     记录总数
    /// </summary>
    public int TotalCount { get; set; }

    /// <summary>
    ///     总页码数
    /// </summary>
    public int PageCount => TotalCount / PageSize + 1;
}